package cn.aj.pet.service.impl;

import cn.aj.pet.domain.PetType;
import cn.aj.pet.mapper.PetTypeMapper;
import cn.aj.pet.service.IPetTypeService;
import cn.aj.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aj
 * @since 2022-12-03
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {


    /**
    * @Title: tree
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/12/3 0003 17:37  
    * @Parameters: []
    * @Return java.util.List<cn.aj.pet.domain.PetType>
     * 无限极树
    */
    @Override
    public List<PetType> tree() {
        List<PetType> petTypeList = super.loadAll();
        ArrayList<PetType> list = new ArrayList<PetType>(); // 新集合装无限树
        for (PetType petType : petTypeList) {
            if(petType.getPid() == null) { // 最大的爹
                list.add(petType);
            } else {
                for (PetType children : petTypeList) { // 找儿子
                    if(children.getId().equals(petType.getPid())) {
                        children.getChildren().add(petType);
                        break;
                    }
                }
            }
        }
        return list;
    }
}
